#ifndef _EVENT_H_
#define _EVENT_H_
#include "event_type.h"
#if defined(WIN32) && !defined(__MINGW32__)
#include <boost/cstdint.hpp>
using namespace boost;
#else
#include <stdint.h>
#endif
#include <string.h>

const int MAX_EVENT_LENGTH = 4096;

struct BaseEvent {
	uint16_t size;
	uint16_t type;
};

struct RequestRegisterUser: public BaseEvent {
	RequestRegisterUser()
	{
		size = sizeof(*this);
		type = EET_REGISTER_USER;
		guid = 0;
	}
	uint8_t local_ip[4];
	uint16_t port;
	uint16_t _dummy; // for aligning
	uint64_t guid;
};

struct RespondRegisterUser: public BaseEvent {
	RespondRegisterUser()
	{
		size = sizeof(*this);
		type = EET_RESPOND_REGISTER_USER;
	}
	uint32_t state; // 0 = OK;
	uint64_t guid;
};

struct RequestRegisterUrl: public BaseEvent {
	RequestRegisterUrl()
	{
		size = sizeof(*this);
		type = EET_REGISTER_URL;
	}
	void makeSize() {
		size = sizeof(*this) + strlen(url) + 1;
	}
	char url[2000];
};

struct RespondRegisterUrl: public BaseEvent {
	RespondRegisterUrl() {
		size = sizeof(*this);
		type = EET_RESPOND_REGISTER_URL;
	}
	int32_t state;
};

struct RequestMirror: public BaseEvent {
	RequestMirror()
	{
		size = sizeof(*this);
		type = EET_REQUEST_MIRROR;
	}
	void makeSize()
	{
		size = sizeof(BaseEvent) + strlen(url) + 1;
	}
	char url[2000]; // Placeholder
};

struct RespondMirror: public BaseEvent {
	RespondMirror()
	{
		size = sizeof(*this);
		type = EET_RESPOND_MIRROR;
	}
	void makeSize()
	{
		size = sizeof(BaseEvent) + strlen(url) + 1;
	}
	char url[2000];
};

struct RequestFriend: public BaseEvent {
	RequestFriend()
	{
		size = sizeof(*this);
		type = EET_REQUEST_FRIEND;
	}
	void makeSize()
	{
		size = sizeof(*this) + strlen(url) + 1;
	}
	char url[2000];
};

struct NetPeer
{
	uint8_t global_ip[4];
	uint8_t local_ip[4];
	uint16_t port;
	uint16_t _dummy[2]; // aligning.
};

struct RespondFriend: public BaseEvent {
	RespondFriend() {
		size = sizeof(*this);
		type = EET_RESPOND_FRIEND;
	}
	int32_t peer_count;
	NetPeer peers[20];
};

struct RequestDownloadUrl: public BaseEvent {
	RequestDownloadUrl() {
		size = sizeof(*this);
		type = EET_REQUEST_DOWNLOAD_URL;
	}
	void makeSize() {
		size = sizeof(BaseEvent) + strlen(url) + 1;
	}
	char url[2000];
};

struct RespondDownloadUrl: public BaseEvent {
	RespondDownloadUrl() {
		size = sizeof(*this);
		type = EET_RESPOND_DOWNLOAD_URL;
	}
	uint32_t state;		// 0 means OK, -1 means Error
};

struct RequestFileInfo: public BaseEvent {
	RequestFileInfo() {
		size = sizeof(*this);
		type = EET_REQUEST_FILE_INFO;
	}
};

struct RespondFileInfo: public BaseEvent {
	RespondFileInfo() {
		size = sizeof(*this);
		type = EET_RESPOND_FILE_INFO;
	}
	int64_t file_size;
};

struct RequestDownloadSection: public BaseEvent {
	RequestDownloadSection() {
		size = sizeof(*this);
		type = EET_REQUEST_DOWNLOAD_SECTION;
	}
	uint64_t offset;
	uint64_t length;
};

struct RespondDownloadSection: public BaseEvent {
	RespondDownloadSection() {
		size = sizeof(*this);
		type = EET_RESPOND_DOWNLOAD_SECTION;
	}
	void makeSize() {
		size = sizeof(BaseEvent) + 16 + length;
	}
	uint64_t offset;
	uint64_t length;
	char data[4000];
};

#endif // _EVENT_H_
